﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using INdTFramework.Maps;
using System.Windows.Threading;
using System.Device.Location;
using Microsoft.Phone.Maps.Services;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media.Imaging;

namespace MapSample
{
    public partial class MapWithMyLocalization : PhoneApplicationPage
    {
        // Object to assist in manipulating the map control.
        HelperMap helperMap;

        // Object that will get the current geographical position of the user.
        Geolocation geolocation;

        // Timer.
        DispatcherTimer timer;

        //Object that will receive the current coordinates of the user.
        GeoCoordinate myCoordinates;

        // Object that will receive the destination coordinates of the user.
        GeoCoordinate myDestination;

        public MapWithMyLocalization()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            MyMap.Visibility = Visibility.Collapsed;
            Loading.Visibility = Visibility.Visible;

            // Creation of an instance of the class HelperMap with the map control that is inserted into the view.
            helperMap = new HelperMap(MyMap);

            // Creation of an instance of the class Geolocation.
            geolocation = new Geolocation();

            // Instantiation of the timer.
            // It's created to check whether object 'geolocation' already obtained valid coordinates of the user.
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (geolocation.IsFinished)
            {
                if (geolocation.Status != System.Device.Location.GeoPositionStatus.Disabled &&
                    geolocation.Status != System.Device.Location.GeoPositionStatus.NoData)
                {
                    // If the coordinates of the user were obtained successfully, the timer is stopped and the request to calculate the route will be taken.
                    timer.Stop();
                    GetRoute();
                }
            }
        }

        void GetRoute()
        {
            // Instantiation of the object with the coordinates of the user.
            myCoordinates = new GeoCoordinate(geolocation.Latitude, geolocation.Longitude);

            // Instantiation of the object with the destination coordinates.
            myDestination = new GeoCoordinate(-3.12702496536076, -60.0169777404517);

            var coordinates = new List<GeoCoordinate>();

            // Addition of the coordinates that will be used to calculate route.
            coordinates.Add(myCoordinates);
            coordinates.Add(myDestination);

            // Request for route calculation between the coordinates that are in 'coordinates'.
            // The ShowRoute function is required to execute some action with the resulting route.
            // The travel mode and route optimization are optional parameters.
            helperMap.TraceRoute(coordinates, ShowRoute);
        }

        void ShowRoute(Route route)
        {
            // Addition the route on the map control.
            // The object 'route' contains information such as the length in meters of the route and estimated time of arrival.
            helperMap.AddRoute(route);

            // Image that represents the marker of the user.
            var startMarker = new Image { Source = new BitmapImage(new Uri("/Assets/pin1.png", UriKind.Relative)) };

            // Marker position in relation coordinates of the user.
            var startPositionOrigin = new Point(0.5, 0.5);

            // Image that represents the destination marker of the user.
            var destinationMarker = new Image { Source = new BitmapImage(new Uri("/Assets/pin2.png", UriKind.Relative)) };

            // Marker position in relation destination coordinates.
            var destinationPositionOrigin = new Point(0.5, 1);

            // Addition of the marker of the user on the map control.
            helperMap.AddMarker(myCoordinates, startMarker, startPositionOrigin);

            // Addition of the destination marker on the map control.
            helperMap.AddMarker(myDestination, destinationMarker, destinationPositionOrigin);

            // Definition of the center of the map control from the midpoint between two coordinates.
            helperMap.SetCenter(myCoordinates, myDestination);

            Loading.Visibility = Visibility.Collapsed;
            MyMap.Visibility = Visibility.Visible;
        }
    }
}